﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using WebTemplate.DAL.Repositories;
using WebTemplate.DAL.Entities;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace WebTemplate.ControlPanel
{
    public partial class Newslist : System.Web.UI.Page
    {
        #region Events


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                FillResult();
            }
        }

        protected void gvNewsResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.PageIndex = e.NewPageIndex;
            FillResult();
        }

        protected void gvNewsResult_Sorting(object sender, GridViewSortEventArgs e)
        {
            this.SortExpression = e.SortExpression;
            this.PageIndex = 0;
            FillResult();
        }

        protected void gvNewsResult_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteItem")
            {
                int rowindex = int.Parse(e.CommandArgument.ToString());
                int newsId = (int)gvNewsResult.DataKeys[rowindex].Value;
                NewsRepository NewsRepository = new NewsRepository();
                NewsRepository.Delete(newsId);
                FillResult();

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            NewsSearchCriteria searchCriteria = new NewsSearchCriteria();
            NewsRepository repository = new NewsRepository();

            if (!string.IsNullOrWhiteSpace(txtTitle.Text))
                searchCriteria.Title = txtTitle.Text.Trim();

            DateTime temp;
            if (DateTime.TryParse(txtDate.Text, out temp))
                searchCriteria.Date = temp;

            if (!string.IsNullOrWhiteSpace(txtEditor.Text))
                searchCriteria.CreatedBy = int.Parse(txtEditor.Text);

            gvNewsResult.DataSource = repository.Select(searchCriteria);
            gvNewsResult.DataBind();

            //if (txtDate.Text == "" && txtEditor.Text == "")
            //{
            //    c.Title = txtTitle.Text;
            //    List<News> result = d.Select(c);
            //    gvNewsResult.DataSource = result;
            //    gvNewsResult.DataBind();
            //}
            //else if (txtEditor.Text == "" && txtTitle.Text == "")
            //{
            //    c.Date = Convert.ToDateTime(txtDate.Text);
            //    List<News> result = d.Select(c);
            //    gvNewsResult.DataSource = result;
            //    gvNewsResult.DataBind();
            //}
            //else if (txtDate.Text == "" && txtTitle.Text == "")
            //{
            //    c.CreatedBy = Convert.ToInt32(txtEditor.Text);
            //    List<News> result = d.Select(c);
            //    gvNewsResult.DataSource = result;
            //    gvNewsResult.DataBind();
            //}
            //else if (txtTitle.Text == "")
            //{
            //    c.Date = Convert.ToDateTime(txtDate.Text);
            //    c.CreatedBy = Convert.ToInt32(txtEditor.Text);
            //    List<News> result = d.Select(c);
            //    gvNewsResult.DataSource = result;
            //    gvNewsResult.DataBind();
            //}
            //else if (txtDate.Text == "")
            //{
            //    c.Title = txtTitle.Text;
            //    c.CreatedBy = Convert.ToInt32(txtEditor.Text);
            //    List<News> result = d.Select(c);
            //    gvNewsResult.DataSource = result;
            //    gvNewsResult.DataBind();
            //}
            //else if (txtEditor.Text == "")
            //{
            //    c.Title = txtTitle.Text;
            //    c.Date = Convert.ToDateTime(txtDate.Text);
            //    List<News> result = d.Select(c);
            //    gvNewsResult.DataSource = result;
            //    gvNewsResult.DataBind();
            //}
            //else
            //{
            //    c.Title = txtTitle.Text;
            //    c.Date = Convert.ToDateTime(txtDate.Text);
            //    c.CreatedBy = Convert.ToInt32(txtEditor.Text);
            //    List<News> result = d.Select(c);
            //    gvNewsResult.DataSource = result;
            //    gvNewsResult.DataBind();
            //}

        }

        #endregion

        #region

        private NewsSearchCriteria SearchCriteria
        {
            get
            {
                return this.ViewState["SearchCriteria"] as NewsSearchCriteria;
            }
            set
            {
                this.ViewState["SearchCriteria"] = value;
            }
        }
        private string SortExpression
        {
            set
            {
                this.ViewState["SortExpression"] = value;
            }
            get
            {
                return this.ViewState["SortExpression"] as string;
            }
        }
        private int PageIndex
        {
            set
            {
                this.ViewState["PageIndex"] = value;
            }
            get
            {
                return this.ViewState["PageIndex"] == null ? 0 : (int)this.ViewState["PageIndex"];
            }
        }
        private void FillResult()
        {
            var searchCriteria = this.SearchCriteria;
            var sortExpression = this.SortExpression;

            if (searchCriteria == null)
            {
                searchCriteria = new NewsSearchCriteria();
                this.SearchCriteria = searchCriteria;
            }

            if (string.IsNullOrWhiteSpace(sortExpression))
            {
                sortExpression = this.SortExpression = "Id";
            }
            NewsRepository repository = new NewsRepository();
            IEnumerable<News> result = repository.Select(searchCriteria);

            if (sortExpression == "Title")
                result = result.OrderBy(x => x.Title).ToList();
            else if (sortExpression == "ShortDescription")
                result = result.OrderBy(x => x.ShortDescription).ToList();
            else if (sortExpression == "Date")
                result = result.OrderBy(x => x.Date).ToList();

            gvNewsResult.PageIndex = this.PageIndex;
            gvNewsResult.DataSource = result.ToList();
            gvNewsResult.DataBind();
        }
        #endregion

    }
}